package switchviz.graphic.gauge.tacho.scale;

import java.awt.Dimension;

import javax.swing.Box;

import org.jdom.Element;

import switchviz.core.ResourceManager;
import switchviz.graphic.IGraphicsPanel;
import switchviz.gui.component.JColorPanel;
import switchviz.gui.component.JMaskedTextField;

public class TachoScalePanel extends IGraphicsPanel
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2368874200523625113L;

	private TachoScalePersistence _xmlhandler = null;
	
	private JMaskedTextField _txtscalestart = null;
	private JMaskedTextField _txtangle = null;
	private JMaskedTextField _txtdepth = null;
	private JMaskedTextField _txtsmallticks = null;
	private JMaskedTextField _txtbigticks = null;
	private JMaskedTextField _txtbigticklength = null;
	private JMaskedTextField _txtsmallticklength = null;
	private JMaskedTextField _txtbigtickwidth = null;
	private JMaskedTextField _txtsmalltickwidth = null;
	private JColorPanel _clscalepanel = null;
	private JColorPanel _cllabelpanel = null;
	

	public TachoScalePanel(Element root)
	{
		super();
		initialize();
		
		setElement(root);
	}
	
	private void initialize()
	{
		this.setName("Tacho Scale");
		Dimension dimlabelwidth = new Dimension(200, 20);
		
		_txtscalestart = new JMaskedTextField("Start of Scale: ", JMaskedTextField.FLOATNUM, dimlabelwidth);
		_txtangle = new JMaskedTextField("Angle of Scale: ", JMaskedTextField.FLOATNUM, dimlabelwidth);
		_txtdepth = new JMaskedTextField("Depth: ", JMaskedTextField.FLOATNUM, dimlabelwidth);
		_txtsmallticks = new JMaskedTextField("Number of Small Ticks: ", JMaskedTextField.INTNUM, dimlabelwidth);
		_txtbigticks = new JMaskedTextField("Number of Big Ticks: ", JMaskedTextField.INTNUM, dimlabelwidth);
		_txtbigticklength = new JMaskedTextField("Length Big Ticks: ", JMaskedTextField.FLOATNUM, dimlabelwidth);
		_txtsmallticklength = new JMaskedTextField("Length Small Ticks: ", JMaskedTextField.FLOATNUM, dimlabelwidth);
		_txtbigtickwidth = new JMaskedTextField("Width Big Ticks: ", JMaskedTextField.FLOATNUM, dimlabelwidth);
		_txtsmalltickwidth = new JMaskedTextField("Width Small Ticks: ", JMaskedTextField.FLOATNUM, dimlabelwidth);
		_clscalepanel = new JColorPanel("set Color for Scale: ", dimlabelwidth);
		_cllabelpanel = new JColorPanel("set Color for Labels: ", dimlabelwidth);
		
		this.add(_txtscalestart);
		this.add(_txtangle);
		this.add(_txtdepth);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_txtbigticks);
		this.add(_txtbigticklength);
		this.add(_txtbigtickwidth);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_txtsmallticks);
		this.add(_txtsmallticklength);
		this.add(_txtsmalltickwidth);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_clscalepanel);
		this.add(_cllabelpanel);
		//color.g
	}
	
	public Element getElement()
	{
		TachoScaleDataModel datamodel = new TachoScaleDataModel();
		
		datamodel.setScaleStart(Float.valueOf(_txtscalestart.getText()));
		datamodel.setAngle(Float.valueOf(_txtangle.getText()));
		datamodel.setDepth(Float.valueOf(_txtdepth.getText()));
		datamodel.setNumbigticks(Integer.valueOf(_txtbigticks.getText()));
		datamodel.setNumsmallticks(Integer.valueOf(_txtsmallticks.getText()));
		datamodel.setLengthbigticks(Float.valueOf(_txtbigticklength.getText()));
		datamodel.setLengthsmallticks(Float.valueOf(_txtsmallticklength.getText()));
		datamodel.setWidthbigticks(Float.valueOf(_txtbigtickwidth.getText()));
		datamodel.setWidthsmallticks(Float.valueOf(_txtsmalltickwidth.getText()));
		datamodel.setScaleColor(_clscalepanel.getColor());
		datamodel.setLabelColor(_cllabelpanel.getColor());
		
		return _xmlhandler.getElement(datamodel);
	}

	public void setElement(Element node) 
	{
		_xmlhandler = new TachoScalePersistence(node);
		
		TachoScaleDataModel datamodel = _xmlhandler.getDataModel();
		
		_txtscalestart.setText(String.valueOf(datamodel.getScaleStart()));
		_txtangle.setText(String.valueOf(datamodel.getAngle()));
		_txtdepth.setText(String.valueOf(datamodel.getDepth()));
		_txtsmallticks.setText(String.valueOf(datamodel.getNumsmallticks()));
		_txtbigticks.setText(String.valueOf(datamodel.getNumbigticks()));
		_txtbigticklength.setText(String.valueOf(datamodel.getLengthbigticks()));
		_txtsmallticklength.setText(String.valueOf(datamodel.getLengthsmallticks()));
		_txtbigtickwidth.setText(String.valueOf(datamodel.getWidthbigticks()));
		_txtsmalltickwidth.setText(String.valueOf(datamodel.getWidthsmallticks()));
		_clscalepanel.setColor(datamodel.getScaleColor());
		_cllabelpanel.setColor(datamodel.getLabelColor());
	}	
}
